package cn.myframe.utils;

import cn.myframe.entity.sys.SysBaseDataEntity;
import cn.myframe.entity.sys.SysParameterEntity;
import cn.myframe.log.LogUtils;
import cn.myframe.service.sys.SysBaseDataService;
import cn.myframe.service.sys.SysParameterService;
import cn.myframe.shiro.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;

//import cn.myframe.redis.RedisUtil;

@Component(value = "baseDataUtils")
public class BaseDataUtils {
    @Autowired
    private SysBaseDataService sysBaseDataService;
    @Autowired
    private SysParameterService sysParameterService;
   /* @Autowired
    private RedisUtil redisUtil;*/
    
    public static String LANGUAGE_TYPE = "LANGUAGE";
    
    public static Map<String, List<SysBaseDataEntity>>  BASE_DATA_MAP = new LinkedHashMap<>();
    
    public static Map<String,Map<String, List<SysBaseDataEntity>>> I18n_Date_MAP=new HashMap<String,Map<String, List<SysBaseDataEntity>>>();
    //系统参数    
    public static Map<String,String> PARAMETER = new HashMap<String,String>();
    
    @PostConstruct
    public void getAllBaseData(){
        List<SysBaseDataEntity> baseDataList = sysBaseDataService.selectAllBaseData();
        List<String> allType = sysBaseDataService.selectAllType();
        List<String> i18n = sysBaseDataService.selectI18n();
        for(SysBaseDataEntity baseData : baseDataList){
        	for(String language : i18n){
        		if(I18n_Date_MAP.containsKey(language)){
        			putTypeData( baseData ,  allType,  language);
        		}else{
        			Map<String, List<SysBaseDataEntity>> dataMap = new HashMap<String, List<SysBaseDataEntity>>();
        			I18n_Date_MAP.put(language, dataMap);
        			putTypeData( baseData ,  allType,  language);
        		}
        	}
        }
    } 
    
    /**
     * 系统参数
     */
    @PostConstruct
    @Async
    public void getParameterData(){
    	Map<String, Object> map = new HashMap<>();
    	map.put("status", "2");
    	List<SysParameterEntity> list = sysParameterService.queryList(map);
    	if(list != null){
    		for(SysParameterEntity entity : list){
    			if ("logLevel".equals(entity.getParamKey())) {
					LogUtils.setAllLogLevel(entity.getParamValue());
				}
    			PARAMETER.put(entity.getParamKey(), entity.getParamValue());
    			//redisUtil.updateSysParameter(entity.getParamKey(), entity.getParamValue());
        	}
    	}
    }
    
    public void putTypeData(SysBaseDataEntity baseData , List<String> types, String language){
    	Map<String, List<SysBaseDataEntity>> dataMap = I18n_Date_MAP.get(language);
		if(baseData.getLanguage().equals(language)){
			for(String type : types){
				if(dataMap.containsKey(type)){
					if(baseData.getType().equals(type)){
						dataMap.get(type).add(baseData);
						break;
					}
					
				}else{
					 List<SysBaseDataEntity> list = new ArrayList<>();
					 dataMap.put(type, list);
					 if(baseData.getType().equals(type)){
						 list.add(baseData);
						 break;
					 }
				}
			}
		}
    	
    }
    
    /**
     * 获取国际化的值
     * @param language
     * @param type
     * @param key
     * @return
     */
    public static String getValue(String type,String key){
    	String language = ShiroUtils.getUserEntity().getLanguage();
    	try{
    		Map<String, List<SysBaseDataEntity>> map = I18n_Date_MAP.get(language);
        	if(map != null){
        		List<SysBaseDataEntity> list =  map.get(type);
        		if(list != null){
        			for(SysBaseDataEntity entity : list){
        				if(entity.getKey().equals(key)){
        					return entity.getValue();
        				}
        			}
        		}
        	}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
    	return "";
    }
    
    /**
     * 根据类型获取MAP
     * @param type
     * @return
     */
    public static Map<String,String> getMap(String type){
    	String language = ShiroUtils.getUserEntity().getLanguage();
    	Map<String,String> resultMap = new HashMap<String,String>();
    	try{
    		Map<String, List<SysBaseDataEntity>> map = I18n_Date_MAP.get(language);
        	if(map != null){
        		List<SysBaseDataEntity> list =  map.get(type);
        		if(list != null){
        			for(SysBaseDataEntity entity : list){
        				resultMap.put(entity.getKey(), entity.getValue());
        			}
        		}
        	}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return resultMap;
    }
    
    /**
     * 根据类型获取MAP,以实体的value为map的主键，实体的key为value
     * @param type
     * @return
     */
    public static Map<String,String> getMapReverse(String type){
    	String language = ShiroUtils.getUserEntity().getLanguage();
    	Map<String,String> resultMap = new HashMap<String,String>();
    	try{
    		Map<String, List<SysBaseDataEntity>> map = I18n_Date_MAP.get(language);
        	if(map != null){
        		List<SysBaseDataEntity> list =  map.get(type);
        		if(list != null){
        			for(SysBaseDataEntity entity : list){
        				resultMap.put(entity.getValue(),entity.getKey());
        			}
        		}
        	}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return resultMap;
    }
    
    
    public static String getEntityData(String key){
    	return getValue("entityData", key);
    }
    
    public static String getSysterm(String key){
    	return getValue("systerm", key);
    }
    
    public static String getSyster(String key , String language){
    	try{
    		Map<String, List<SysBaseDataEntity>> map = I18n_Date_MAP.get(language);
        	if(map != null){
        		List<SysBaseDataEntity> list =  map.get("systerm");
        		if(list != null){
        			for(SysBaseDataEntity entity : list){
        				if(entity.getKey().equals(key)){
        					return entity.getValue();
        				}
        			}
        		}
        	}
    	}catch(Exception e){
    		e.printStackTrace();
    	}    	
    	return "";
    }
    
  
}
